/***************************************************************************//**
 * @file CGI/SkyDomeLight.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef SKY_DOME_LIGHT_CPP
#define SKY_DOME_LIGHT_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <osg/Light>
#include <osg/PositionAttitudeTransform>

#include <CGI/NodeCallback.h>
#include <CGI/SkyDome.h>
#include <CGI/SkyDomeLight.h>

////////////////////////////////////////////////////////////////////////////////

const double SkyDomeLight::m_distance = SkyDome::m_radius - 0.005 * SkyDome::m_radius;

////////////////////////////////////////////////////////////////////////////////

SkyDomeLight::SkyDomeLight( CGI_Data *data ) :
    m_data( data )
{
    m_skyDomeLightGroup = createSkyDomeLightGroup();

    m_skyDomeLightData = new DataTypeSkyDomeLight( m_skyDomeLightGroup.get() );

    m_skyDomeLightGroup->setUserData( m_skyDomeLightData.get() );
    m_skyDomeLightGroup->setUpdateCallback( new NodeCallback<DataTypeSkyDomeLight> );
}

////////////////////////////////////////////////////////////////////////////////

SkyDomeLight::~SkyDomeLight() {}

////////////////////////////////////////////////////////////////////////////////

void SkyDomeLight::update()
{
    m_skyDomeLightData->setElevation( m_data->sunData.elevation );
}

////////////////////////////////////////////////////////////////////////////////

osg::ref_ptr<osg::Group> SkyDomeLight::createSkyDomeLightGroup()
{
    osg::ref_ptr<osg::Group> skyDomeLightGroup = new osg::Group();
    skyDomeLightGroup->setName( "SkyDomeLightGroup" );

    osg::ref_ptr<osg::PositionAttitudeTransform> lightPAT = new osg::PositionAttitudeTransform();
    lightPAT->setName( "SkyDomeLightPAT" );

    osg::ref_ptr<osg::StateSet> stateSet = skyDomeLightGroup->getOrCreateStateSet();

    osg::ref_ptr<osg::LightSource> lightSource = createLightSource( 0 );

    lightSource->setLocalStateSetModes( osg::StateAttribute::ON );
    lightSource->setStateSetModes( *stateSet, osg::StateAttribute::ON );

    lightPAT->addChild( lightSource.get() );

    skyDomeLightGroup->addChild( lightPAT.get() );

    return skyDomeLightGroup;
}

////////////////////////////////////////////////////////////////////////////////

osg::ref_ptr<osg::LightSource> SkyDomeLight::createLightSource( int num )
{
    osg::ref_ptr<osg::LightSource> lightSource = new osg::LightSource();
    lightSource->setName( "SkyDomeLightSource" );

    osg::ref_ptr<osg::Light> light = new osg::Light();
    light->setName( "SkyDomeLight" );

    light->setLightNum( num );

    light->setAmbient( osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) );
    light->setDiffuse( osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) );

    light->setConstantAttenuation( 1.0 );

    lightSource->setLight( light.get() );

    lightSource->getLight()->setPosition( osg::Vec4( -SkyDomeLight::m_distance, 0.0, 0.0, 0.0 ) );

    return lightSource;
}
